/*
 *      Author: Honrun
 */
#ifndef _DevicesKey_H_
#define _DevicesKey_H_



#define KEY_UP_GPIO_Port        GPIOB
#define KEY_UP_Pin              GPIO_PIN_4

#define KEY_DOWN_GPIO_Port      GPIOC
#define KEY_DOWN_Pin            GPIO_PIN_14

#define KEY_LEFT_GPIO_Port      GPIOB
#define KEY_LEFT_Pin            GPIO_PIN_7

#define KEY_RIGHT_GPIO_Port     GPIOB
#define KEY_RIGHT_Pin           GPIO_PIN_6

#define KEY_SELECT_GPIO_Port    GPIOC
#define KEY_SELECT_Pin          GPIO_PIN_15

#define KEY_START_GPIO_Port     GPIOD
#define KEY_START_Pin           GPIO_PIN_0

#define KEY_A_GPIO_Port         GPIOD
#define KEY_A_Pin               GPIO_PIN_1

#define KEY_B_GPIO_Port         GPIOB
#define KEY_B_Pin               GPIO_PIN_2

#define KEY_X_GPIO_Port         GPIOA
#define KEY_X_Pin               GPIO_PIN_2

#define KEY_Y_GPIO_Port         GPIOA
#define KEY_Y_Pin               GPIO_PIN_3

#define KEY_HOME_GPIO_Port      GPIOC
#define KEY_HOME_Pin            GPIO_PIN_13


#define KEY_HOME                0x10000
#define KEY_B                   0x08000
#define KEY_Y                   0x04000
#define KEY_A                   0x00080
#define KEY_X                   0x00040
#define KEY_SELECT              0x00020
#define KEY_START               0x00010
#define KEY_UP                  0x00008
#define KEY_DOWN                0x00004
#define KEY_LEFT                0x00002
#define KEY_RIGHT               0x00001
#define KEY_ALL                 (KEY_HOME | KEY_X | KEY_Y | KEY_A | KEY_B | KEY_SELECT | KEY_START | KEY_UP | KEY_DOWN | KEY_LEFT | KEY_RIGHT)


/* 持续按键间隔时间ms */
#define KEY_CONTINUE_INTERVAL_TIME      100
/* 持续按键启动时间ms */
#define KEY_CONTINUE_TIME               2000
/* 长按键时间ms */
#define KEY_LONG_TIME                   1000
/* 短按键时间ms */
#define KEY_SHORT_TIME                  50


typedef enum {
    keyNormal       = 0,

    keyAdd          = 0x0001,           /* 增加了新按键 */
    keyDel          = 0x0002,           /* 减少了旧按键 */
    keyAddAndDel    = 0x0004,           /* 即减少了旧按键并且又增加了新按键 */
    keyEqual        = 0x0008,           /* 当前按键值与上次按键值相同 */

    keyShort        = 0x0100,           /* 短按 */
    keyLong         = 0x0200,           /* 长按 */
    keyContinuous   = 0x0400,           /* 持续按 */
    keyUp           = 0x0800,           /* 松开 */
}KeyStateEnum;


typedef struct
{
    uint32_t (*uiKeyValueGet)(void);    /* 按键键值读取函数 */

    uint32_t valueLast;                 /* 当前状态的按下键值 */
    uint32_t valueDown;                 /* 新增的按下键值 */
    uint32_t valueUp;                   /* 新增的松开键值 */

    uint32_t timeDown;                  /* 最后一次按下的时刻 */
    uint32_t timeUp;                    /* 最后一次松开的时刻 */

    KeyStateEnum state;                 /* 状态 */
    uint8_t cntCombo;                   /* 连击次数 */
}KeyType;


extern KeyType g_typeKey;


void vKeyInit(void);
uint32_t uiKeyValueGet(void);
KeyStateEnum enumKeyMachine(KeyType *ptypeKey);


#endif
